package sg.edu.nus.iss.billing.vo.subscription.feature;

import sg.edu.nus.iss.billing.common.DbUtil;
import sg.edu.nus.iss.billing.common.Util;
import sg.edu.nus.iss.billing.common.exception.InvalidParamsException;


public class IDDCallFeatureVO extends FeatureVO {
	
	private double perSecRate;
	
	public IDDCallFeatureVO() {
		// Default Parameters
		setName(Util.FEATUREVO_IDDCALL);
		setStartDate(Util.getNullDate());
		setEndDate(Util.getNullDate());
		setPerSecRate(0);
		setDescription(Util.Default_NULL);
	}
	
	public double getPerSecRate() {
		return perSecRate;
	}

	public void setPerSecRate(double perSecRate) {
		this.perSecRate = perSecRate;
	}

	@Override
	public void setParameters(String serialisedEntry)
			throws InvalidParamsException {
		String[] tokens =  serialisedEntry.split(java.util.regex.Pattern.quote(DbUtil.DELIMITER));
		if (tokens.length!=5 ) {
			throw new InvalidParamsException("Expected 5, received " + tokens.length);
		} else {
			//name^startDate^endDate^rate^description
			setName(tokens[0]);
			setStartDate(Util.getDateFromString(tokens[1]));
			setEndDate(Util.getDateFromString(tokens[2]));
			setPerSecRate(Double.parseDouble(tokens[3]));
			setDescription(tokens[4]);
		}		
	}

	@Override
	public String serialised() {
		StringBuilder sb = new StringBuilder();
		sb.append(getName());
		sb.append(DbUtil.DELIMITER);
		sb.append(Util.getFormattedDate(getStartDate()));
		sb.append(DbUtil.DELIMITER);
		sb.append(Util.getFormattedDate(getEndDate()));
		sb.append(DbUtil.DELIMITER);
		sb.append(getPerSecRate());
		sb.append(DbUtil.DELIMITER);
		sb.append(getDescription());
		return sb.toString();
	}
}
